package com.hf.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hf.entity.Modules;
import com.hf.entity.Sign;
import com.hf.entity.UserRoles;
import com.hf.entity.Users;
import com.hf.entity.UsersParam;
import com.hf.service.IModulesService;
import com.hf.service.IUserRolesService;
import com.hf.service.IUsersService;
import com.hf.util.Result;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author eight
 * @since 2021-06-06
 */
@RestController
@RequestMapping("/users")
public class UsersController {
	@Autowired
	IUsersService iUsersService;
	@Autowired
	IModulesService iModulesService;
	@Autowired
	IUserRolesService iUserRolesService;

	/**
	 * 分页查询咨询师
	 * 
	 * @param userParam
	 * @return
	 */
	@RequestMapping("/getZiXunShi")
	public Object getZiXunShi(UsersParam userParam) {
		List<Users> list = iUsersService.getziZunShi(userParam);
		userParam.setPage(null);
		userParam.setLimit(null);
		int num = (iUsersService.getziZunShi(userParam)).size();
		return new Result(0, "查询成功", list, num);
	}
	@RequestMapping("/getQianZiXunShi")
	public Object getZiXunShi2(UsersParam userParam) {
		List<Users> list = iUsersService.getQianziZunShi(userParam);
		
		return list;
	}
	/**
	 * 修改咨询师的权重
	 * 
	 * @param user
	 * @return
	 */

	@RequestMapping("/updateWeight")
	public Object updateWeight(Users user) {
		boolean res = iUsersService.saveOrUpdate(user);
		if (res) {
			return new Result(0, "修改成功");
		}
		return new Result(1, "修改失败");
	}

	/**
	 * 多条件分页查询 localhost:8080/users/getList?page=0&limit=10
	 * 
	 * @param u
	 * @return
	 */
	@RequestMapping("/getList")
	public Object getList(UsersParam u) {

		IPage<Users> p = iUsersService.getList(u);

		List<Users> list = p.getRecords();
		int num = (int) p.getTotal();

		return new Result(0, "查詢成功", list, num);

	}

	/**
	 * 添加 localhost:8080/users/insert?loginName=赵云&password=111111&isLockOut=0
	 * 
	 * @param u
	 * @return
	 */
	@PostMapping("/insert")
	public Object insert(Users u) {
		// LocalDateTime localDateTime = LocalDateTime.now();
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		u.setCreatTime(sdf.format(date));
		u.setIsQiandao("未签到");
		if (iUsersService.save(u)) {
			return new Result(0, "添加成功");
		}
		return new Result(1, "添加失败");
	}

	/**
	 * 删除 localhost:8080/users/delete?id=1
	 * 
	 * @param id
	 * @return
	 */
	@PostMapping("/delete")
	public Object delete(Integer id) {
		List<UserRoles> list = iUserRolesService.getListByUserId(id);
		System.out.println(list);
		System.out.println(list.size());
		if (list.size() == 0) {
			if (iUsersService.removeById(id)) {
				return new Result(0, "删除成功");
			}
			return new Result(1, "删除失败");
		} else {
			return new Result(1, "该用户不能删除");
		}

	}

	/**
	 * 修改 localhost:8080/users/update?loginName=张飞&id=2
	 * 
	 * @param u
	 * @return
	 */
	@PostMapping("/update")
	public Object update(Users u) {

		if (iUsersService.saveOrUpdate(u)) {
			return new Result(0, "修改成功");
		}
		return new Result(1, "修改失败");
	}

	/**
	 * 锁定
	 * 
	 * @param loginName
	 * @return
	 */
	@PostMapping("/setLock")
	public Object setLock(@RequestParam Integer id) {
		boolean res = iUsersService.setLock(id);
		if (res) {
			return new Result(0, "锁定成功");
		}
		return new Result(1, "锁定失败");

	}

	/**
	 * 解锁
	 * 
	 * @param loginName
	 * @return
	 */
	@PostMapping("/setUnLock")
	public Object setUnLock(@RequestParam Integer id) {
		boolean res = iUsersService.setUnLock(id);
		if (res) {
			return new Result(0, "解锁成功");
		}
		return new Result(1, "解锁失败");

	}

	/**
	 * 重置密码
	 * 
	 * @param id
	 * @return
	 */
	@PostMapping("/resetPassword")
	public Object resetPassword(@RequestParam("id") Integer id) {

		Users u = new Users();
		u.setId(id);
		u.setPassword("123456");
		if (iUsersService.updateById(u)) {
			return new Result(0, "重置密码成功");
		}
		return new Result(1, "重置密码失败");
	}

	/**
	 * 根据userId获取用户拥有的的模块
	 * 
	 * @param userId
	 * @return
	 */

	@GetMapping("/getModulesTreeByUserId")
	public Object getModulesTreeByUserId(Integer userId) {
		List<Modules> list = iModulesService.getModulesTreeByUserId(userId);
		return list;
	}

	/**
	 * 修改用户密码
	 */
	@RequestMapping("/updatePwd")
	public Object updatePwd(Integer id,String password,String newPassword) {
		System.out.println(password);
		Users byId = iUsersService.getById(id);
		System.out.println(password);
		System.out.println(byId.getPassword());
		if (byId.getPassword().equals(password)) {
			Users u = new Users();
			u.setId(id);
			u.setPassword(newPassword);
			if (iUsersService.updateById(u)) {
				return new Result(0, "修改密码成功");
			}
		}
		return new Result(1, "原密码错误");
	}

}
